/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.utilities;

import java.text.DecimalFormat;

/**
 * The Class SizeUtilities.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class SizeUtilities {

	 /** The Constant KB_BYTES. */
	 public static final long KB_BYTES = new Long(1024).longValue();

	 /** The Constant MB_BYTES. */
	 public static final long MB_BYTES = SizeUtilities.KB_BYTES * new Long(1024).longValue();

	 /** The Constant GB_BYTES. */
	 public static final long GB_BYTES = SizeUtilities.MB_BYTES * new Long(1024).longValue();

	 /** The Constant TB_BYTES. */
	 public static final long TB_BYTES = SizeUtilities.GB_BYTES * new Long(1024).longValue();

	 /** The Constant B_UNIT. */
	 public static final String B_UNIT = "b";

	 /** The Constant KB_UNIT. */
	 public static final String KB_UNIT = "Kb";

	 /** The Constant MB_UNIT. */
	 public static final String MB_UNIT = "Mb";

	 /** The Constant GB_UNIT. */
	 public static final String GB_UNIT = "Gb";

	 /** The Constant TB_UNIT. */
	 public static final String TB_UNIT = "Tb";

	 /**
	  * From bytes to unit.
	  *
	  * @param bytes the bytes
	  *
	  * @return the string
	  */
	 public static String fromBytesToUnit(double bytes) {

			bytes = Math.abs(bytes);

			if (bytes < SizeUtilities.KB_BYTES) {
				 return SizeUtilities.B_UNIT;
			} else if (bytes < SizeUtilities.MB_BYTES) {
				 return SizeUtilities.KB_UNIT;
			} else if (bytes < SizeUtilities.GB_BYTES) {
				 return SizeUtilities.MB_UNIT;
			} else if (bytes < SizeUtilities.TB_BYTES) {
				 return SizeUtilities.GB_UNIT;
			} else {
				 return SizeUtilities.TB_UNIT;
			}
	 }

	 /**
	  * From bytes to value.
	  *
	  * @param bytes the bytes
	  *
	  * @return the double
	  */
	 public static double fromBytesToValue(double bytes) {
			return SizeUtilities.fromBytesToValue(bytes, SizeUtilities.fromBytesToUnit(bytes));
	 }

	 /**
	  * From bytes to value.
	  *
	  * @param bytes the bytes
	  * @param unit the unit
	  *
	  * @return the double
	  */
	 public static double fromBytesToValue(double bytes, String unit) {

			if (unit.equals(SizeUtilities.B_UNIT)) {
				 return bytes;
			} else if (unit.equals(SizeUtilities.KB_UNIT)) {
				 return bytes / SizeUtilities.KB_BYTES;
			} else if (unit.equals(SizeUtilities.MB_UNIT)) {
				 return bytes / SizeUtilities.MB_BYTES;
			} else if (unit.equals(SizeUtilities.GB_UNIT)) {
				 return bytes / SizeUtilities.GB_BYTES;
			} else if (unit.equals(SizeUtilities.TB_UNIT)) {
				 return bytes / SizeUtilities.TB_BYTES;
			}

			return bytes;
	 }

	 /**
	  * From bytes to string.
	  *
	  * @param bytes the bytes
	  *
	  * @return the string
	  */
	 public static String fromBytesToString(double bytes) {
			final DecimalFormat decimalFormat = new DecimalFormat("###,###,###,##0.##");
			return SizeUtilities.fromBytesToString(bytes, decimalFormat);

	 }

	 /**
	  * From bytes to string.
	  *
	  * @param bytes the bytes
	  * @param decimalFormat the decimal format
	  *
	  * @return the string
	  */
	 public static String fromBytesToString(double bytes, DecimalFormat decimalFormat) {
			return decimalFormat.format(SizeUtilities.fromBytesToValue(bytes)) + " " + SizeUtilities.fromBytesToUnit(bytes);
	 }

	 public static double fromBytesToPercentage(double current, double total) {
			double percentage;

			if (total > 0 && current > 0) {
				 percentage = new Double(current) / new Double(total);
			} else {
				 if (current > total) {
						percentage = 1.0d;
				 } else {
						percentage = 0.0d;
				 }
			}

			return percentage;
	 }
}
